package com.zlkj.pro.modular.wechat.service.impl;

import cn.stylefeng.roses.kernel.config.api.context.ConfigContext;
import com.zlkj.pro.modular.wechat.model.param.SendTemplateMsgParam;
import com.zlkj.pro.modular.wechat.service.WeChatService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceHttpClientImpl;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class WxChatServiceImpl extends WxMpServiceHttpClientImpl implements WeChatService {

    @Autowired
    private WxMpService wxMpService;

    /**
     * 发送模板消息
     * @param param
     */
    @Override
    public void send(SendTemplateMsgParam param){
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(param.getToUser())
                .templateId(param.getTemplateId())
                .url(param.getUrl())
                .data(param.getData())
                .miniProgram(new WxMpTemplateMessage.MiniProgram(param.getAppid(), param.getPagePath(), Boolean.FALSE))
                .build();
        log.info("SendTemplateMsgParam============>{}",param);
        try {
            wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            log.info("templateMessage============>{}",templateMessage);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
    }


    /**
     * 批量发送模板消息
     * @param params
     */
    @Override
    public void send(List<SendTemplateMsgParam> params){

        for (SendTemplateMsgParam param : params) {
            log.info("param============>{}",param);
            this.send(param);
        }
    }

}
